import * as _ from './common';
import jQuery from 'jquery';
const document = window.document;

/**
 * 日期字段选择限制范围
 * @param {fieldid} 	日期字段ID
 * @param {floorday} 	下限天数(与当前日期比较)，空值代表不限制
 * @param {upperday} 	上限天数(与当前日期比较)，空值代表不限制
 * @return 返回true表示在范围内，false表示超出限定范围或日期置为空
 */
window.cus_judgeDateRange = (fieldid, floorday, upperday) =>{
	const fieldval = window.WfForm.getFieldValue(fieldid);
	if(!!!fieldval)		
		return false;
	floorday = (floorday === "" ? -99999 : parseInt(floorday));
	upperday = (upperday === "" ? 99999 : parseInt(upperday));
	const curdate = new Date();
	const defdate = curdate.getFullYear()+"-"+(curdate.getMonth()+1)+"-"+curdate.getDate();
	let diffdays = (new Date(fieldval.replace(/-/g, "/")).getTime() - new Date(defdate.replace(/-/g, "/")).getTime())/(24*3600*1000);
	diffdays = Math.ceil(diffdays);
	if(diffdays >= floorday && diffdays <= upperday)
		return true;
	else
		return false;
}

/**
 * 计算时间差
 * @param {begfields} 开始时间
 * @param {endfields} 结束时间
 * @param {unit} 	计量结果单位1(天)、2(小时)、3(分钟)、4(秒)，默认为天
 * @param {digit} 	计算结果保留小数位数，默认2位小数
 * @return 以endfields字段值减去beginfields字段值得到时间差值
 * 注：begfields/endfields结构，可为单独日期字段、单独时间字段、日期+时间组合字段(以英文逗号隔开);日期字段空值默认取今天，时间字段空值默认为00:00
 */
window.cus_CalTimeDiff = (begfields, endfields, unit, digit) =>{
	if(!!!begfields || !!!endfields)
		return "ParamsError";
	const diffmillsecond = window.cus_GetTimeDiffMillSecond(begfields, endfields);
	const diffsecond = diffmillsecond/1000;
	unit = !!unit ? parseInt(unit) : 1;
	digit = (!!digit || digit == 0) ? parseInt(digit) : 2;
	let retdiff = 0;
	if(unit === 1)
		retdiff = diffsecond/(3600*24);
	else if(unit === 2)
		retdiff = diffsecond/3600;
	else if(unit === 3)
		retdiff = diffsecond/60;
	else if(unit === 4)
		retdiff = diffsecond;
	if(digit === 0)
		retdiff = Math.floor(retdiff);	//向下取整
	else
		retdiff = retdiff.toFixed(digit);
	return retdiff;
}

/**
 * 比较timefield1与timefield2两字段时间大小
 * @param {timefield1} 字段1(开始时间)
 * @param {timefield2} 字段2(结束时间)
 * @return 返回true表示timefield2较大，false表示timefield1较大或相等
 * 注：timefield1/timefield2结构，可为单独日期字段、单独时间字段、日期+时间组合字段(以英文逗号隔开);日期字段空值默认取今天，时间字段空值默认为00:00
 */
window.cus_CompareTime = (timefield1, timefield2) =>{
	const diffmillsecond = window.cus_GetTimeDiffMillSecond(timefield1, timefield2);
	return diffmillsecond > 0;
}

//计算两时间字段相差毫秒数
window.cus_GetTimeDiffMillSecond = (field1, field2) =>{
	const timeval1 = window.cus_getTimeValueByField(field1);
	const timeval2 = window.cus_getTimeValueByField(field2);
	const diffmillsecond = new Date(timeval2.replace(/-/g, "/")).getTime() - new Date(timeval1.replace(/-/g,"/")).getTime();
	return diffmillsecond;
}

//根据日期/时间字段ID取标准格式时间值
window.cus_getTimeValueByField = (fields) =>{
	let timeval;
	const fieldArr = fields.split(",");
	if(fieldArr.length === 1)
		timeval = window.WfForm.getFieldValue(fieldArr[0]);
	else if(fieldArr.length > 1)
		timeval = window.WfForm.getFieldValue(fieldArr[0]) + " " + window.WfForm.getFieldValue(fieldArr[1]);
	const curdate = new Date();
	const defdate = curdate.getFullYear()+"-"+(curdate.getMonth()+1)+"-"+curdate.getDate();
    if(timeval.indexOf("-") == -1)
        timeval = defdate + " " + timeval;
	return timeval;
}

/**
 * 显示指定区域
 * @param {areanames} 自定义属性中设置的name属性区域集合，多个以逗号隔开
 */
window.cus_ShowAreaByName = (areanames) =>{
    const arr = areanames.split(",");
    arr && arr.map(item =>{
        window.jQuery("[name="+item+"]").show().removeClass("filter_required");
    });
}

/**
 * 隐藏指定区域，隐藏的区域不校验必填
 * @param {areanames} 自定义属性中设置的name属性区域集合，多个以逗号隔开
 */
window.cus_HideAreaByName = (areanames) =>{
    const arr = areanames.split(",");
    arr && arr.map(item =>{
        window.jQuery("[name="+item+"]").hide().addClass("filter_required");
    });
}

/**
 * 控制明细列隐藏/显示
 * @param {cusclassname}  列自定义class属性
 * @param {status}  1为显示，2为隐藏
 */
window.cus_ControlDetailColumnByClass = (cusclassname, status) =>{
	if(status === 1)
		window.jQuery("td."+cusclassname).show();
	else if(status === 2)
		window.jQuery("td."+cusclassname).hide();
}

/**
 * 校验字段是否必填，不满足弹出提示
 * @param {fieldid} 需校验的字段ID，多个以逗号分隔
 * @param {alertmsg} 存在未必填字段时弹出提示信息，空则不提示
 * @return true代表存在未填写字段，false表示校验通过
 */
window.cus_verifyExistNullField = (fieldids, alertmsg) =>{
    let existNullField = false;
    const fieldArr = fieldids.split(",");
    fieldArr && fieldArr.map(fieldid =>{
        const fieldInfo = window.WfForm.getFieldInfo(fieldid.substring(5));
        if(_.isEmpty(fieldInfo) || fieldInfo.htmltype === 4)
			return true;
		const fieldval = window.WfForm.getFieldValue(fieldid);
		if(fieldval === ""){
			existNullField = true;
			return false;
		}
    });
	if(existNullField && !!alertmsg)
		alert(alertmsg);
	return existNullField;
}

/**
 * 根据字段ID取字段值
 * @param {fieldid} 字段ID
 * @return 字段对应值
 * 注：支持文本、多行文本框、浏览框(ID值)、选择框、check框(选中为1未选中为0)
 */
window.cus_getFieldValue = (fieldid) =>{
	return window.WfForm.getFieldValue(fieldid);
}

/**
 * 给文本字段赋值，支持只读/编辑情况
 * @param {fieldid} 字段ID
 * @param {fieldvalue}	字段值 
 */
window.cus_setInputFieldValue = (fieldid, fieldvalue) =>{
    window.WfForm.changeFieldValue(fieldid, {value:fieldvalue});
}

window.$G = (identity, _document) =>{
	return window.$GetEle(identity, _document);
}

window.$GetEle = (identity, _document) =>{
	let rtnEle = null;
	if (_document == undefined || _document == null) 
		_document = document;
	rtnEle = _document.getElementById(identity);
	if (rtnEle == undefined || rtnEle == null) {
		rtnEle = _document.getElementsByName(identity);
		if (rtnEle.length > 0) 
			rtnEle = rtnEle[0];
		else 
			rtnEle = null;
	}
	return rtnEle;
}

window.$GetEles = (identity) =>{
	let rtnEle = null;
	rtnEle = document.getElementsByName(identity);
	if (rtnEle.length == 1) {
		return rtnEle[0]; 
	} else if (rtnEle.length == 0) {
		return document.getElementById(identity);
	}
	return rtnEle;
}